<?php
/**
 * class App_Plugin_Common
 * common hooks
 *
 * @author kim
 */
class App_Plugin_Common extends Zend_Controller_Plugin_Abstract
{
    /**
     * assign layout variables for global usage
     *
     * @return null
     */
    protected function _assignLayoutVariables()
    {
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
        if ($bootstrap->hasPluginResource('Layout')) {
            // get layout object
            $bootstrap->bootstrap('Layout');
            $layout = $bootstrap->getResource('Layout');

            $userModel = new App_Model_User();

            // assign variables to default
            if ($myUser = App_Model_User::getStoredMyUser()) {
                define('IS_USER_LOGGED_IN', true);

                $myUserId = $myUser['id'];
                $layout->assign('myUser', $myUser);
                $layout->assign('myUserId', $myUserId);
            } else {
                define('IS_USER_LOGGED_IN', false);
            }
        }
    }

    /**
     * hook for route shutdown
     * most of the things can be done within this hook
     *
     * @param Zend_Controller_Request_Abstract
     * @return null
     */
    public function routeShutdown(Zend_Controller_Request_Abstract $request)
    {
        // default timezone
        //date_default_timezone_set('America/Los_Angeles');

        /**
         * get current module name
         */
        define('CURRENT_MODULE', $request->getModuleName());

        /**
         * get bootstrap object and it's options
         */
        $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap');
        $options = $bootstrap->getOptions();

        /**
         * get layout object
         */
        if ($bootstrap->hasPluginResource('Layout')) {
            $bootstrap->bootstrap('Layout');
            $layout = $bootstrap->getResource('Layout');

            /**
             * set module layout
             */
            if (isset($options[CURRENT_MODULE]['resources']['layout']['layoutPath'])) {
                $layout->setLayoutPath($options[CURRENT_MODULE]['resources']['layout']['layoutPath']);
            }
            if (isset($options[CURRENT_MODULE]['resources']['layout']['layout'])) {
                $layout->setLayout($options[CURRENT_MODULE]['resources']['layout']['layout']);
            }
        }

        /**
         * get view object
         */
        if ($bootstrap->hasPluginResource('View')) {
            $bootstrap->bootstrap('View');
            $view = $bootstrap->getResource('View');

            /**
             * web url for view scripts
             */
            define('WEB_URL', $view->baseUrl());

            if (!App_Model_User::setStoredMyUser()) {
                $view->formLogin = new App_Form_User_Login();
            }
        }

        /**
         * define the upload paths
         */
        if (isset($options['uploadpath']['user'])) {
            define('UPLOAD_PATH_USER', $options['uploadpath']['user']);
        }
        if (isset($options['uploadpath']['place'])) {
            define('UPLOAD_PATH_PLACE', $options['uploadpath']['place']);
        }
        if (isset($options['uploadpath']['blog'])) {
            define('UPLOAD_PATH_BLOG', $options['uploadpath']['blog']);
        }
        if (isset($options['uploadpath']['help'])) {
            define('UPLOAD_PATH_HELP', $options['uploadpath']['help']);
        }

    }

    /**
     * hook for dispatch loop startup
     *
     * @param Zend_Controller_Request_Abstract
     * @return null
     */
    public function dispatchLoopStartup(Zend_Controller_Request_Abstract $request)
    {
        $this->_assignLayoutVariables();
    }


}